<?php

declare(strict_types=1);

namespace App\Common\Models;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Model\Model;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\Contract\TranslatorInterface;
use App\Common\Exception\Store\DataNotFound;
use App\Common\Demand\Server\Utils;
class BaseModel extends Model
{
    #[Inject]
    protected  TranslatorInterface $translatorInterface;
    #[Inject]
    protected  Utils $utils;
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    const DELETED_AT = 'delete_time';
    use SoftDeletes;
    protected ?string $dateFormat = 'U';
    /**
     * 获取用户的姓名.
     *
     * @param  string  $value
     * @return string
     */
    public function getCreateTimeAttribute($value)
    {
        return date("Y-m-d H:i",(int)$value);
    }
    public function saveAdd($addData):bool{
        return (bool)$this->create($addData);
    }
    public function getRow($where, array $field=['*']):array{
        $row=$this->query()->where($where)->first($field);
        if(empty($row)){
            return [];
        }else{
            return $row->toArray();
        }
    }
    public function saveEdit($where,$editData):bool{
        $row=$this->getRow($where);
        return (bool)$this->query()->where($where)->update($editData);
    }
    public function saveDel($where):bool{
        $row=$this->getRow($where);
        return $this->query()->where($where)->delete();
    }
    public function getList(array $where, array $field=['*']):array{
        $listPage=$this->query()->where($where)->get($field);
        if($listPage->isEmpty()){
            throw new DataNotFound($this->translatorInterface->trans('admin.dataNotFind'));
        }else{
            return $listPage->toArray();
        }
    }
}
